package org.point.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.point.common.controller.BaseController;
import org.point.service.TaskService;
import org.point.common.util.DoPage;
import org.point.domain.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @ClassName: ProductInOutController  
 * @Description: TODO(这里用一句话描述这个类的作用)  
 * @author Kenny_Z' szqiuk@gmail.com
 * @date 2016年10月23日 下午9:18:39  
 *
 */
@Controller
@RequestMapping("/Task/")
public class TaskController extends BaseController {
	
	@Autowired
	private TaskService service;
	
	@RequestMapping(value = "taskList.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void taskList(@RequestParam(value = "memberId")String memberId, @RequestParam(value = "currentPage") Integer currentPage, HttpServletResponse response){
		
		JSONObject jsonObj = new JSONObject();
		DoPage<Task> page = new DoPage<Task>();
		Map<String, Object> map = new HashMap<String, Object>();
		
		try {
			if(memberId != null){
				map.put("memberId", memberId);
				if(currentPage != null && currentPage > 0){
					
					page.setPageSize(10);
					page.setCurrentPage(currentPage);
					map.put("firstRecord", page.getFirstRecord());
					map.put("lastRecord", page.getLastRecord());
					page.setMap(map);
					// 查询记录数
					Integer totalRecords = service.countTask(page);
					page.setTotalRecords(totalRecords);
					// 查询记录
					List<Task> records = service.searchTask(page);
					page.setList(records);
					// 返回分页参数，及积分规则分类数据
					jsonObj.put("page", page);
					// 查询积分规则成功
					jsonObj.put("resultMsg", 1);
				} else {
					// 分页当前页为null
					jsonObj.put("resultMsg", -2);
				}
			} else {
				// 会员单位ID为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", 0);
			throw new RuntimeException("查询积分规则分类错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (IOException e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
		
	}
}
